package com.zj.utils.view.image;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.zj.utils.photoview.PhotoView;
import com.zj.utils.utils.utilcode.ImageUtils;

import java.util.ArrayList;


public class IntruderViewPagerAdapter extends PagerAdapter {
    private Context context;
    private ArrayList<String> mList;
    private int positions;

    public IntruderViewPagerAdapter(Context context, ArrayList<String> mList, int position) {
        this.context = context;
        this.mList = mList;
        this.positions = position;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view==o;
    }

    private boolean isFull = false;

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        if (mList != null && position < mList.size()) {
            String resId = mList.get(position);
            if (resId != null) {
                PhotoView popupImages = new PhotoView(context);
                Glide.with(context)
                        .load(mList.get(position))
                        .into(popupImages);
                popupImages.setImageBitmap(ImageUtils.compressByQuality(BitmapFactory.decodeFile(mList.get(position)),20));
                popupImages.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!isFull){
                            isFull = true;
                            PopupReviewImage.getInstance().closeActionbar();
                        }else {
                            isFull = false;
                            PopupReviewImage.getInstance().showActionbar();
                        }
                        //ToastUtils.showShort(mList.get(positions));
                    }
                });

                //此处假设所有的照片都不同，用resId唯一标识一个itemView；也可用其它Object来标识，只要保证唯一即可
                popupImages.setTag(null);

                container.addView(popupImages);
                return popupImages;
            }
        }
        return null;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        int count = container.getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = container.getChildAt(i);
            if (childView == object) {
                container.removeView(childView);
                break;
            }
        }
    }
}
